package com.cms.admin.action.healthrecord;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.GoodsDataStuffRemark;
import com.cms.admin.model.GoodsDataStuffRemarkExt;
import com.cms.admin.model.UserList;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.healthrecord.GoodsDataStuffRemarkService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ModelDriven;

public class GoodsDataStuffRemarkAction extends BaseAction implements ModelDriven<GoodsDataStuffRemarkExt>{

    private TemplateService templateService;
    private GoodsDataStuffRemarkService goodsDataStuffRemarkService;
    private GoodsDataStuffRemarkExt search = new GoodsDataStuffRemarkExt();

    public String ajaxQueryGoodsDataStuffRemarks()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        List<GoodsDataStuffRemark> goodsDataStuffRemarks=goodsDataStuffRemarkService.queryGoodsDataStuffRemarkList(search);

        String res = null;
        try {
            res = getQueryResult(goodsDataStuffRemarks);
            JSONObject o = new JSONObject();
            o.put("errorCode", 0);
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            LOG.error("ajaxQueryUnGoingOrder", e);
            JSONObject o = new JSONObject();
            o.put("errorCode", -1);
            o.put("errorCode", Constants.INFO_SYS_ERROR);
            return ajaxJsonSuccessMessage(o);
        }
    }

    private String getQueryResult(List<GoodsDataStuffRemark> goodsDataStuffRemarkList)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("goodsDataStuffRemarkList", goodsDataStuffRemarkList);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_HEALTH_RECORDS_STUFFREMARK_QUERY_RESULT, map);
        return result;
    }

    public String ajaxAddGoodsDataStuffRemark()
    {

        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        search.setUpdateDate(new Date());
        search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
        goodsDataStuffRemarkService.addGoodsDataStuffRemark(search);
        JSONObject o = new JSONObject();
        if (search.getRecid()>0)
        {
            o.put("errorCode", 0);
            o.put("errorMsg", "success");
        }else
        {
            o.put("errorCode", -1);
            o.put("errorMsg", "新增记录失败");
        }
        return ajaxJsonSuccessMessage(o);
    }

    public String ajaxDeleteGoodsDataStuffRemark()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        goodsDataStuffRemarkService.deleteGoodsDataStuffRemark(search.getRecid());
        GoodsDataStuffRemark stuffRemark=goodsDataStuffRemarkService.queryGoodsDataStuffRemarkById(search.getRecid());
        JSONObject o = new JSONObject();
        if (null==stuffRemark)
        {
            o.put("errorCode", 0);
            o.put("errorMsg", "success");
        }else
        {
            o.put("errorCode", -1);
            o.put("errorMsg", "新增记录失败");
        }
        return ajaxJsonSuccessMessage(o);
    }

    public String ajaxUpdateGoodsDataStuffRemark()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        search.setUpdateDate(new Date());
        search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
        goodsDataStuffRemarkService.updateGoodsDataStuffRemark(search);
        JSONObject o = new JSONObject();
        o.put("errorCode", 0);
        o.put("errorMsg", "success");

        return ajaxJsonSuccessMessage(o);
    }


    public String ajaxQueryGoodsStuffRemarkForUpdate()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        GoodsDataStuffRemark stuffRemark=goodsDataStuffRemarkService.queryGoodsDataStuffRemarkById(search.getRecid());
        String res=null;

        try {
            res=getQueryResultForUpdate(stuffRemark);

            JSONObject o = new JSONObject();
            o.put("errorCode", 0);
            o.put("errorMsg", "success");
            o.put("content",res);
            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            LOG.error("ajaxQueryGoodsStuffRemarkForUpdate", e);
            JSONObject o = new JSONObject();
            o.put("errorCode", -1);
            o.put("errorCode", Constants.INFO_SYS_ERROR);
            return ajaxJsonSuccessMessage(o);
        }

    }

    private String getQueryResultForUpdate(GoodsDataStuffRemark goodsDataStuffRemark) throws Exception
    {
        initBathPath();
        HashMap map = new HashMap();
        map.put("goodsDataStuffRemark", goodsDataStuffRemark);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_HEALTH_RECORDS_STUFFREMARK_FORUPDATE_QUERY_RESULT, map);
        return result;
    }

    public GoodsDataStuffRemarkExt getModel() {
        return search;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public GoodsDataStuffRemarkService getGoodsDataStuffRemarkService() {
        return goodsDataStuffRemarkService;
    }

    public void setGoodsDataStuffRemarkService(GoodsDataStuffRemarkService goodsDataStuffRemarkService) {
        this.goodsDataStuffRemarkService = goodsDataStuffRemarkService;
    }

    public GoodsDataStuffRemarkExt getSearch() {
        return search;
    }

    public void setSearch(GoodsDataStuffRemarkExt search) {
        this.search = search;
    }
}
